package cetcbigdata.da.personal_service.sichuan;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class SiChuan extends Base {
    private static String baseRegion = "四川省";

    private static String baseModule = "个人服务";
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                for(JSONObject m : getModule()){
                    if (m.getString("code").length() > 0){
                        this.parseListPage(recordSender, fieldsList, r, m);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem) {
        //获取区名和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");
        //获取各个市的区和类型编码
        String regCode = regionItem.getString("regCode");
        String code = moduleItem.getString("code");
        int pageno = 1;
        try {
            //没有数据返回时，网站中会出现 div[@class='noitem'] 元素，则停止翻页
            while (true){
                String listUrl = "http://www.sczwfw.gov.cn/jiq/interface/item/tags?dxType=2&areaCode="
                        + regCode + "&theme=" + code + "&deptCode=&isonline=&pageno=" + pageno + "&type=1&limitSceneNum=&taskType=";
                JSONObject header = new JSONObject();
                header.put("Referer", "http://www.sczwfw.gov.cn/jiq/front/item/gr_index?areaCode=" + regCode);
                header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0");
                JSONObject params = new JSONObject();

                String response = getReq(listUrl, header, params);
                JXDocument jxDocument = new JXDocument(response);
                List<Object> firstTitleList = jxDocument.sel("//div[@class='sx_list']");
                for (Object o : firstTitleList){
                    JSONObject detailHeader = new JSONObject();
                    JSONObject detailParams = new JSONObject();
                    JSONObject content = new JSONObject();
                    String detailUrl = null;
                    String detailResponse = null;
                    String firstTitle = null;
                    List<JSONObject> xuanxiang = new ArrayList<>();
                    // 判断是否有子选项
                    Elements elements = ((Element) o).getElementsByTag("ul");
                    if(elements.size() > 0){
                        // 有子选项
                        Elements lis = ((Element) o).child(1).children();
                        for(Element li : lis){
                            JSONObject xuanxiangItem = new JSONObject();
                            String secondTitle = li.child(0).attr("title");
                            String url = li.child(0).attr("onclick");
                            url = url.replace("ywblurl", "").replace("(", "").
                                    replace(")", "").replace("'", "").replace("'", "");
                            String[] detail = url.split(",");
                            detailUrl = detail[0];
                            //请求详情页
                            detailResponse = getReq(detailUrl, detailHeader, detailParams);
                            xuanxiangItem.put("名称", secondTitle);
                            xuanxiangItem.put("信息", getDetailPage(detailResponse));
                            xuanxiang.add(xuanxiangItem);
                        }
                    }
                    else {
                        JSONObject xuanxiangItem = new JSONObject();
                        String url = ((Element) o).child(0).child(0).child(0).attr("onclick");
                        url = url.replace("ywblurl", "").replace("(", "").
                                replace(")", "").replace("'", "").replace("'", "");
                        String[] detail = url.split(",");
                        detailUrl = detail[0];
                        //请求详情页
                        detailResponse = getReq(detailUrl, detailHeader, detailParams);
                        xuanxiangItem.put("信息", getDetailPage(detailResponse));
                        xuanxiang.add(xuanxiangItem);
                    }
                    firstTitle = ((Element) o).child(0).child(0).attr("title");
                    content.put("名称", firstTitle);
                    content.put("选项", xuanxiang);
                    processResult(
                            listUrl, response, detailUrl, detailResponse, content, region, module
                    );
                }
                if (jxDocument.sel("//div[@class='sx_list']").size() == 0){
                    break;
                }
                pageno ++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    private static JSONObject getDetailPage(String html){
        JXDocument jxDocument = new JXDocument(html);
        JSONObject content = new JSONObject();
        content.put("基本信息", SCUtil.getBaseInfo(jxDocument));
        content.put("审批结果", SCUtil.getApprovalInfo(jxDocument));
        content.put("窗口办理", SCUtil.getWinHandling(jxDocument));
        content.put("网上办理", SCUtil.getOnlineProcess(jxDocument));
        content.put("常规信息", SCUtil.getGeneralInfo(jxDocument));
        content.put("申请材料", SCUtil.getApplicationMaterialsInfo(jxDocument));
        content.put("受理条件", SCUtil.getAcceptance(jxDocument));
        content.put("办理流程", SCUtil.getProcess(jxDocument));
        content.put("收费标准", SCUtil.getCharges(jxDocument));
        content.put("设定依据", SCUtil.getSettingBasis(jxDocument));
        content.put("中介服务",  SCUtil.getAgentService(jxDocument));
        content.put("常见问题", SCUtil.getQuestionInfo(jxDocument));
        return content;
    }

    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{ name: '阿坝藏族羌族自治州', value: '513200000000', areaId: '949' },\n" +
                "{ name: '巴中市', value: '511900000000', areaId: '1995' },\n" +
                "{ name: '成都市', value: '510100000000', areaId: '1447' },\n" +
                "{ name: '达州市', value: '511700000000', areaId: '504' },\n" +
                "{ name: '德阳市', value: '510600000000', areaId: '1499' },\n" +
                "{ name: '甘孜藏族自治州', value: '513300000000', areaId: '1512' },\n" +
                "{ name: '广安市', value: '511600000000', areaId: '1709' },\n" +
                "{ name: '广元市', value: '510800000000', areaId: '408' },\n" +
                "{ name: '乐山市', value: '511100000000', areaId: '890' },\n" +
                "{ name: '凉山彝族自治州', value: '513400000000', areaId: '329' },\n" +
                "{ name: '泸州市', value: '510500000000', areaId: '774' },\n" +
                "{ name: '眉山市', value: '511400000000', areaId: '1419' },\n" +
                "{ name: '绵阳市', value: '510700000000', areaId: '1746' },\n" +
                "{ name: '内江市', value: '511000000000', areaId: '1588' },\n" +
                "{ name: '南充市', value: '511300000000', areaId: '1199' },\n" +
                "{ name: '攀枝花市', value: '510400000000', areaId: '386' },\n" +
                "{ name: '遂宁市', value: '510900000000', areaId: '1477' },\n" +
                "{ name: '雅安市', value: '511800000000', areaId: '1028' },\n" +
                "{ name: '宜宾市', value: '511500000000', areaId: '1996' },\n" +
                "{ name: '资阳市', value: '512000000000', areaId: '1011' },\n" +
                "{ name: '自贡市', value: '510300000000', areaId: '661' }\n" +
                "]";
        return JSONArray.parseArray(city);
    }

    public static List<JSONObject> getRegion(){
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("value");

            //请求获取各市包含的区的名称和编码
            String url = "http://www.sczwfw.gov.cn/jiq/interface/jitem/find-by-code?code=" + code + "&deptType=1&jsonpCallback=jsonpcallback&_=1664338799123";
            JSONObject header = new JSONObject();
            JSONObject params = new JSONObject();

            String response = getReq(url, header, params);
            //处理数据获取区名和编码
            String data = response.replace("jsonpcallback", " ").replace("(", " ").replace(")", " ");
            JSONObject responseObj = JSONObject.parseObject(data);
            if(responseObj.getString("code").equals("200")){
                JSONArray resList = responseObj.getJSONObject("data").getJSONObject("group").getJSONArray("sonAreas");
                for (Object r : resList){
                    JSONObject rr = (JSONObject) r;
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name") + "-" + rr.getString("name");
                    outItem.put("name", name);
                    outItem.put("regCode", rr.getString("areaCode"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        List<JSONObject> out = new ArrayList<>();
        String url = "http://www.sczwfw.gov.cn/jiq/front/item/gr_index?areaCode=510000000000";
        JSONObject header = new JSONObject();
        JSONObject params = new JSONObject();

        String response = getReq(url, header, params);
        JXDocument document = new JXDocument(response);
        List<Object> elments = document.sel("//ul[@id='themeList']/li");
        //跳过第一项不遍历
        for (int i = 1; i < elments.size(); i++) {
            JSONObject item = new JSONObject();
            String info = ((Element) elments.get(i)).child(0).attr("onclick");
            info = info.replace("changeinfoL(", "").replace(")", "");
            String [] infoArr = info.split(",");
            String codeStr = infoArr[0];
            String code = codeStr.replace("'", "").replace("'", "");
            item.put("name", infoArr[3]);
            item.put("code", code);
            out.add(item);
        }


        return out;
    }

    public static void main(String[] args) {
        SiChuan spiders = new SiChuan();
        spiders.getData(null, new ArrayList<>());
    }
}
